package com.lenovo.newsapp.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.adobe.mobile.Analytics;
import com.adobe.mobile.Config;
import com.lenovo.newsapp.R;
import com.lenovo.newsapp.model.ReadCategory;
import com.lenovo.newsapp.util.Const;
import com.lenovo.newsapp.util.PreferenceUtil;
import com.worklight.wlclient.api.WLClient;
import com.worklight.wlclient.api.WLFailResponse;
import com.worklight.wlclient.api.WLProcedureInvocationData;
import com.worklight.wlclient.api.WLResponse;
import com.worklight.wlclient.api.WLResponseListener;

public class CategoryActivity extends BaseActivity{
	
	private ArrayList<ReadCategory> resultList = new ArrayList<ReadCategory>();
	
	private RadioGroup categoryRG;
	
	private int localCategoryId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_category_list);
		actionBar.setTitle(R.string.setting_readingpreferences);
		
		localCategoryId = PreferenceUtil.getInt("checkedRadioBtn",0);
		
		categoryRG = (RadioGroup) findViewById(R.id.category_rg);
		categoryRG.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				Log.v("checkedId", checkedId+"");
				Log.v("group", group.getCheckedRadioButtonId()+"");
				Log.v("group", PreferenceUtil.getInt("checkedRadioBtn",0)+"");
				if(group.getCheckedRadioButtonId() == R.id.all_category){
					PreferenceUtil.commitInt("checkedRadioBtn", 0);
				}else{
					PreferenceUtil.commitInt("checkedRadioBtn", group.getCheckedRadioButtonId());
				}
				if(localCategoryId != group.getCheckedRadioButtonId()){
					finish();
					localCategoryId = group.getCheckedRadioButtonId();
				}
			}
		});
		
//		RadioButton radioButton = new RadioButton(this);
//		radioButton.setText("ddd");
//		categoryRG.addView(radioButton);
		addRadioBtn();
		
		Analytics.trackState("CategoryActivity", null);
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Config.pauseCollectingLifecycleData();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Config.collectLifecycleData();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			Const.isFrefresh=false;
		finishWithAnimation();
		return true;
	}
		return super.onOptionsItemSelected(item);
		
	}

	private void addRadioBtn() {
		
		String adapterName = "NewsAppAdapter";
		String procedureName = "categoryList";
		WLProcedureInvocationData invocationData = new WLProcedureInvocationData(
				adapterName, procedureName);
		Object[] parameters = new Object[] {};
		invocationData.setParameters(parameters);

		LoginActivity.client = WLClient.getInstance();
		LoginActivity.client.invokeProcedure(invocationData,
				new WLResponseListener() {

					public void onSuccess(WLResponse response) {
						final JSONObject jsonObj = response.getResponseJSON();
						Log.v(Const.DEBUG, "onSuccess--dataJson = " + jsonObj);
						handler.post(new Runnable() {

							@Override
							public void run() {
								try {
//									showResult(jsonObj,"like");
									if(jsonObj.getInt("status_code") == 0){
										JSONArray result = jsonObj.getJSONArray(Const.RESULT);
										Log.v(Const.DEBUG, "onSuccess--result = " + result);
//										RadioButton radioButton_1 = new RadioButton(CategoryActivity.this);
//										radioButton_1.setText(R.string.all_category);
//										categoryRG.addView(radioButton_1);
										for(int i=0;i<result.length();i++){
											JSONObject item = result.getJSONObject(i);
											
											RadioButton radioButton = new RadioButton(CategoryActivity.this);
											radioButton.setText(item.getString("category"));
											radioButton.setId(item.getInt("category_id"));
											if(PreferenceUtil.getInt("checkedRadioBtn",0) == item.getInt("category_id")){
												radioButton.setChecked(true);
											}
											categoryRG.addView(radioButton);
											
										}
//										Toast.makeText(NewsDetailImgActivity.this, R.string.like_success,Toast.LENGTH_LONG).show();
									}else{
//										Toast.makeText(NewsDetailImgActivity.this, R.string.like_failure,Toast.LENGTH_LONG).show();
									}
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
//								hideLoadProgressBar();
							}

						});

					}

					public void onFailure(WLFailResponse response) {
						String responseText = response.getResponseText();
						Log.v(Const.DEBUG, "onFailure--responseText = " + responseText);
						handler.post(new Runnable() {

							@Override
							public void run() {
//								hideLoadProgressBar();
							}

						});
					}

				}, null);
//		new Thread(new Runnable() {
//
//			@Override
//			public void run() {
//				try {
//					resultList = InfoJSONParser.getCommentList(newsId,pageNo);
//					success = true;
//				} catch (Exception e) {
//					success = false;
//					e.printStackTrace();
//				}
//
//				handler.post(new Runnable() {
//
//					@Override
//					public void run() {
//						if (success) {
//							switch (flag) {
//							case Const.INIT_FLAG:
//								setUpListViews();
//								break;
//							case Const.LOAD_MORE_FLAG:
//								loadMoreListView();
//								break;
//							case Const.REFRESH_FLAG:
//								refreshListView();
//							default:
//								break;
//							}
//						} else {
//							showShortToast(R.string.toast_err_download);
//						}
//
//						listview.onHeaderLoadComplete();
//						listview.setCanLoadFooter(canLoadMore);
//						hideLoadProgressBar();
//					}
//
//				});
//			}
//		}).start();
	}
	
	
	
}
